package cn.iocoder.yudao.module.currency.service.rate;

import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.currency.config.PaymentCurrencyConfig;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import cn.iocoder.yudao.module.currency.controller.admin.rate.vo.*;
import cn.iocoder.yudao.module.currency.dal.dataobject.rate.ExchangeRateDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.currency.dal.mysql.rate.ExchangeRateMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.currency.enums.ErrorCodeConstants.*;

/**
 * 货币汇率 Service 实现类
 *
 * @author 超管
 */
@Service
@Validated
public class ExchangeRateServiceImpl implements ExchangeRateService {

    @Resource
    private ExchangeRateMapper exchangeRateMapper;

    @Resource
    private PaymentCurrencyConfig paymentCurrencyConfig;

    @Override
    public Long createExchangeRate(ExchangeRateSaveReqVO createReqVO) {
        // 插入
        ExchangeRateDO exchangeRate = BeanUtils.toBean(createReqVO, ExchangeRateDO.class);
        exchangeRate.setBaseCurrency(paymentCurrencyConfig.getCode());
        exchangeRateMapper.insert(exchangeRate);
        // 返回
        return exchangeRate.getId();
    }

    @Override
    public void updateExchangeRate(ExchangeRateSaveReqVO updateReqVO) {
        // 校验存在
        validateExchangeRateExists(updateReqVO.getId());
        // 更新
        ExchangeRateDO updateObj = BeanUtils.toBean(updateReqVO, ExchangeRateDO.class);
        exchangeRateMapper.updateById(updateObj);
    }

    @Override
    public void deleteExchangeRate(Long id) {
        // 校验存在
        validateExchangeRateExists(id);
        // 删除
        exchangeRateMapper.deleteById(id);
    }

    private void validateExchangeRateExists(Long id) {
        if (exchangeRateMapper.selectById(id) == null) {
            throw exception(EXCHANGE_RATE_NOT_EXISTS);
        }
    }

    @Override
    public ExchangeRateDO getExchangeRate(Long id) {
        return exchangeRateMapper.selectById(id);
    }

    @Override
    public PageResult<ExchangeRateDO> getExchangeRatePage(ExchangeRatePageReqVO pageReqVO) {
        pageReqVO.setBaseCurrency(paymentCurrencyConfig.getCode());
        return exchangeRateMapper.selectPage(pageReqVO);
    }

    @Override
    public ExchangeRateDO getExchangeRate(String baseCurrency, String targetCurrency) {
        LambdaQueryWrapperX<ExchangeRateDO> queryWrapperX = new LambdaQueryWrapperX<ExchangeRateDO>()
                .eq(ExchangeRateDO::getBaseCurrency, baseCurrency)
                .eq(ExchangeRateDO::getCurrencyCode, targetCurrency)
                .eq(ExchangeRateDO::getStatus, CommonStatusEnum.ENABLE.getStatus())
                .eq(ExchangeRateDO::getDeleted, false);
        return exchangeRateMapper.selectOne(queryWrapperX);
    }

}
